package com.army.persondatademo.slice;

import com.army.persondatademo.ResourceTable;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.ability.DataAbilityHelper;
import ohos.aafwk.ability.DataAbilityRemoteException;
import ohos.aafwk.content.Intent;
import ohos.data.dataability.DataAbilityPredicates;
import ohos.data.dataability.DataAbilityUtils;
import ohos.data.rdb.ValuesBucket;
import ohos.data.resultset.ResultSet;
import ohos.hiviewdfx.HiLog;
import ohos.hiviewdfx.HiLogLabel;
import ohos.utils.net.Uri;

public class MainAbilitySlice extends AbilitySlice {
    private static final HiLogLabel LABEL_LOG = new HiLogLabel(3, 0xD001100, "MainAbilitySlice");

    private static final String BASE_URL = "dataability:///com.army.persondatademo.dao.PersonDataAbility";
    private static final String DATA_PATH = "/person";
    private static final String DB_COLUMN_PERSON_ID = "id";
    private static final String DB_COLUMN_NAME = "name";
    private static final String DB_COLUMN_GENDER = "gender";
    private static final String DB_COLUMN_AGE = "age";

    private DataAbilityHelper dataAbilityHelper;

    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_main);

        dataAbilityHelper = DataAbilityHelper.creator(this);

        query();
        insert(100, "Tom", "male", 20);
        insert(101, "Jerry", "female", 21);
        insert(102, "Bob", "male", 22);
        query(); // 查看插入后的结果
        update();
        query(); // 查询更新后的结果
        delete();
        query(); // 查看删除后的结果
    }

    /**
     * 查询所有数据
     */
    private void query() {
        HiLog.info(LABEL_LOG, "Start Query..." );
        String[] columns = new String[] {DB_COLUMN_PERSON_ID, DB_COLUMN_NAME, DB_COLUMN_GENDER, DB_COLUMN_AGE};
        // 构造查询条件
        DataAbilityPredicates dataAbilityPredicates = new DataAbilityPredicates();
        dataAbilityPredicates.between(DB_COLUMN_AGE, 15, 40);

        try {
            ResultSet resultSet = dataAbilityHelper.query(Uri.parse(BASE_URL+DATA_PATH), columns, dataAbilityPredicates);
            if (resultSet == null || resultSet.getRowCount() == 0) {
                HiLog.info(LABEL_LOG, "query: resultSet is null or no result found");
                return;
            }

            resultSet.goToFirstRow(); // 光标移到第一行
            do {
                int id = resultSet.getInt(resultSet.getColumnIndexForName(DB_COLUMN_PERSON_ID));
                String name = resultSet.getString(resultSet.getColumnIndexForName(DB_COLUMN_NAME));
                String gender = resultSet.getString(resultSet.getColumnIndexForName(DB_COLUMN_GENDER));
                int age = resultSet.getInt(resultSet.getColumnIndexForName(DB_COLUMN_AGE));

                HiLog.info(LABEL_LOG, "query: Id :" + id + " Name :" + name + " Gender :" + gender + " Age :" + age);
            }while (resultSet.goToNextRow());

        } catch (DataAbilityRemoteException | IllegalStateException e) {
            e.printStackTrace();
            HiLog.error(LABEL_LOG, "query: dataRemote exception | illegalStateException");
        }

    }

    /**
     * 插入数据
     */
    private void insert(int id, String name, String gender, int age) {
        ValuesBucket valuesBucket = new ValuesBucket();
        valuesBucket.putInteger(DB_COLUMN_PERSON_ID, id);
        valuesBucket.putString(DB_COLUMN_NAME, name);
        valuesBucket.putString(DB_COLUMN_GENDER, gender);
        valuesBucket.putInteger(DB_COLUMN_AGE, age);

        try {
            if (dataAbilityHelper.insert(Uri.parse(BASE_URL+DATA_PATH), valuesBucket) != -1) {
                HiLog.info(LABEL_LOG, "insert successful");
            }
        } catch (DataAbilityRemoteException | IllegalStateException e) {
            e.printStackTrace();
            HiLog.error(LABEL_LOG, "insert: dataRemote exception|illegalStateException");
        }
    }

    /**
     * 更新数据
     */
    private void update() {
        DataAbilityPredicates dataAbilityPredicates = new DataAbilityPredicates();
        dataAbilityPredicates.equalTo(DB_COLUMN_PERSON_ID, 102);

        ValuesBucket valuesBucket = new ValuesBucket();
        valuesBucket.putString(DB_COLUMN_NAME, "ArmyQIn");
        valuesBucket.putInteger(DB_COLUMN_AGE, 38);

        try {
            if (dataAbilityHelper.update(Uri.parse(BASE_URL+DATA_PATH), valuesBucket, dataAbilityPredicates) != -1) {
                HiLog.info(LABEL_LOG, "update successful");
            }
        } catch (DataAbilityRemoteException | IllegalStateException e) {
            e.printStackTrace();
            HiLog.error(LABEL_LOG, "update: dataRemote exception | illegalStateException");
        }
    }

    /**
     * 删除数据
     */
    private void delete() {
        DataAbilityPredicates dataAbilityPredicates = new DataAbilityPredicates().equalTo(DB_COLUMN_PERSON_ID, 100);

        try {
            if (dataAbilityHelper.delete(Uri.parse(BASE_URL+DATA_PATH), dataAbilityPredicates) != -1) {
                HiLog.info(LABEL_LOG, "delete successful");
            }
        } catch (DataAbilityRemoteException | IllegalStateException  e) {
            e.printStackTrace();
            HiLog.error(LABEL_LOG, "delete: dataRemote exception | illegalStateException");
        }
    }


    @Override
    public void onActive() {
        super.onActive();
    }

    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
    }
}
